/********************************** (C) COPYRIGHT *******************************
 * File Name          : CH392SPI_HW.C
 * Author             : WCH
 * Version            : V1.1
 * Date               : 2022/12/27
 * Description        : Hardware abstraction layer for CH392 SPI V1.1
 *******************************************************************************/

#include "CH392SPI_HW.h"
#include "CH392INC.H"
#include "debug.h"
/*******************************************************************************
 * Function Name  : CH392_Port_Init
 * Description    : Initializes the spi port
 * Input          : None
 * Return         : None
 *******************************************************************************/
void CH392_SPIPort_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    SPI_InitTypeDef SPI_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);

    // CS
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_SetBits(GPIOA, GPIO_Pin_2);

    // CLK
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // MISO
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // MOSI
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init(SPI1, &SPI_InitStructure);
    SPI_Cmd(SPI1, ENABLE);
}

/*******************************************************************************
 * Function Name  : SPI1_WriteRead
 * Description    : SPI read and write a byte
 * Input          : u8 Data
 * Return         : None
 *******************************************************************************/
u8 SPI1_WriteRead(u8 data)
{
    u8 retry = 0;

    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
    {
        retry++;
        if (retry > 200)
            return 0;
    }
    SPI_I2S_SendData(SPI1, data);
    retry = 0;

    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
    {
        retry++;
        if (retry > 200)
            return 0;
    }
    return SPI_I2S_ReceiveData(SPI1);
}

/******************************************************************************
 * Function Name  : SPIWriteCH392Cmd
 * Description    : Write CH392 Cmd
 * Input          : u8 cmd
 * Output         : None
 * Return         : None
 *******************************************************************************/
void SPIWriteCH392Cmd(u8 cmd)
{
    SPIEndCH392Cmd();    /* To prevent CS from being low, set CS high first */
    SPICH392CmdStart();  /* Command starts, CS pull down */
    SPI1_WriteRead(cmd); /* The SPI sends the command code */
/*Between the command code and data transmissions, a TSC time delay is required.*/
#ifdef CH392T
    Delay_Us(1);
#endif
#ifdef CH392F
    Delay_Us(3);
#endif
}

/******************************************************************************
 * Function Name  : SPIWriteCH392Data
 * Description    : Write CH392 Data
 * Input          : u8 mdata
 * Output         : None
 * Return         : None
 *******************************************************************************/
void SPIWriteCH392Data(u8 mdata)
{
    SPI1_WriteRead(mdata); /* The SPI sends data  */
/*Between the data and data transmissions, a TSC time delay is required.*/
#ifdef CH392T
    Delay_Us(1);
#endif
#ifdef CH392F
    Delay_Us(3);
#endif
}

/*******************************************************************************
 * Function Name  : SPIReadCH392Data
 * Description    : Read CH392 Data
 * Input          : None
 * Output         : None
 * Return         : u8 i
 *******************************************************************************/
u8 SPIReadCH392Data(void)
{
    u8 data;
    data = SPI1_WriteRead(0x00); /* SPI read Data*/
/*Between the data and data transmissions, a TSC time delay is required.*/
#ifdef CH392T
    Delay_Us(1);
#endif
#ifdef CH392F
    Delay_Us(3);
#endif
    return data;
}
